<?php

namespace Desktopd\MUA;


// CAStore (Stores trusted CAs)
class CAStore {
    protected $certs = array();
    protected $paths = array();
    
    public function __construct () {
        
    }
    
    public function add ($cert) {
        $cert = openssl_x509_read($cert);
        if (!is_resource($cert)) {
            return false;
        }
        
        $certData = '';
        openssl_x509_export($cert, $certData);
        
        if (in_array($certData, $this->certs, true)) {
            return true;
        }
        
        $this->certs[] = $certData;
    }
    
    public function addPath ($path) {
        if (in_array($path, $this->paths, true)) {
            return true;
        }
        
        $this->paths[] = $path;
    }
    
    public function isTrusted ($cert) {
        $cert = openssl_x509_read($cert);
        if (!is_resource($cert)) {
            return false;
        }
        
        $certData = '';
        openssl_x509_export($cert, $certData);
        
        return in_array($certData, $this->certs, true);
    }
    
    public function getAllPaths () {
        return $this->paths;
    }
    
    public function getAll () {
        return $this->certs;
    }
}


// vim: ts=4 et ai

